# coding:utf-8

from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS


def generate_wordcloud(text, save_filename, stopwords={}, mask_ndarray=None):
    """
    输入文本生成词云,如果是中文文本需要先进行分词处理
    """
    # 设置显示方式
    d = path.dirname(__file__)
    font_path = path.join(d, "font", "msyh.ttf")
    stopwords = set.union(set(STOPWORDS), stopwords)
    wc = WordCloud(
        max_words=266,  # 词云显示的最大词数
        mask=mask_ndarray,  # 设置背景图片
        stopwords=stopwords,  # 设置停用词
        font_path=font_path,  # 兼容中文字体，不然中文会显示乱码
        width=4000,
        height=3000,
        background_color=None,  # 设置背景颜色
        mode="RGBA"
    )

    # 生成词云
    wc.generate(text)

    # 生成的词云图像保存到本地
    wc.to_file(path.join(d, "Images", save_filename + ".png"))

    # 显示图像
    plt.imshow(wc, interpolation="bilinear")
    # interpolation='bilinear' 表示插值方法为双线性插值
    plt.axis("off")  # 关掉图像的坐标
    plt.show()
